package org.teremail.schema;

public class PGDialect extends AbstractDialect {

    private static Dialect instance = new PGDialect();
    
    private PGDialect() {}

    public String generateBinary(int length) {
        return "BYTEA";
    }

    public String getInteger(int precision) {
        switch (precision) {
        case 2:
            return "INT2";
        case 4:
            return "INT4";
        case 8:
            return "INT8";
        default:
            throw new RuntimeException("Precision: " + precision + " for integer not supported");
        }
    }
    
    @Override
    public String getBlob() {
        return "OID";
    }
    
    @Override
    public String getTableName(String tableName) {
        return tableName.toLowerCase();
    }
    
    /**
     * @see org.teremail.schema.AbstractDialect#createPagedQuery(java.lang.String, java.lang.String, java.lang.String, java.lang.String, int, int)
     */
    @Override
    public String createPagedQuery(String columns, String tables,
            String constraints, String orderBy, int pageNo, int pageSize) {
        
        return "SELECT " + columns + 
        " FROM " + tables + 
        " WHERE " + constraints + 
        " ORDER BY " + orderBy + 
        " LIMIT " + pageSize + " OFFSET " + (pageNo * pageSize);
        
    }

    /**
     * @see org.teremail.schema.AbstractDialect#isPagingSupported()
     */
    @Override
    public boolean isPagingSupported() {
        return true;
    }
    

    public static Dialect getInstance() {
        return instance;
    }

}
